//
//  VideoDetailViewController.m
//  FNer
//
//  Created by 王展 on 15/10/14.
//  Copyright (c) 2015年 wangzhan. All rights reserved.
//

#import "VideoDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Blur.h"
#import <MediaPlayer/MediaPlayer.h>
#import "DataHandle.h"
#import "BaseMVViewController.h"
#import "MoviePlayerViewController.h"


#import "UINavigationController+animation.h"

@interface VideoDetailViewController ()<UIScrollViewDelegate>

@property(nonatomic,retain)UIScrollView *bigSV;
@property(nonatomic,retain)UIImageView *videoImageView;
@property(nonatomic,retain)UIImageView *bigImageView;
@property(nonatomic,retain)UIButton *startButton;
@property(nonatomic,retain)UILabel *titleLabel;
@property(nonatomic,retain)UILabel *categoryLabel;
@property(nonatomic,retain)UILabel *detailLabel;
@property(nonatomic,retain)UIView *tiaoView;


@property (nonatomic,retain)NSMutableArray *testArray;

//播放视频
@property(nonatomic,retain)BaseMVViewController *moviePlayer;
@property(nonatomic,assign)BOOL shoucang;
@property(nonatomic,retain)Video *myvideo;
@property(nonatomic,retain)Video *video;
@property(nonatomic,retain)UIImage *shoucangImage;
@property(nonatomic,retain)UIImage *quxiaoshoucangImage;
@property(nonatomic,retain)UILabel *atitleLabel;

@property(nonatomic,retain)UIView *tempView;

@end

@implementation VideoDetailViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"视图将要显示");
    
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //初始化
    self.shoucangImage = [UIImage imageNamed:@"app_sc_an.png"];
    self.quxiaoshoucangImage = [UIImage imageNamed:@"app_sc.png"];
    
    //创建title
    self.atitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    self.atitleLabel.text = @"微视";
    self.atitleLabel.font = [UIFont systemFontOfSize:18 * XWIDTH];
    [self.atitleLabel setTextAlignment:NSTextAlignmentCenter];
    self.atitleLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = self.atitleLabel;
    
    NSString *string = [[NSUserDefaults standardUserDefaults] objectForKey:@"nighting"];
    if ([string isEqualToString:@"yes"]) {
#warning 改的颜色
        self.atitleLabel.textColor = [UIColor blackColor];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColoring:) name:@"changeColoring" object:nil];
    //左侧按钮
    UIImage *leftImage = [UIImage imageNamed:@"导航栏_button_back_用于白和灰.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
}
//创建大SV
-(void)createBigSV
{
    self.bigSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HIGHT)];
    self.bigSV.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];

    

      self.bigSV.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
    self.bigSV.contentSize = CGSizeMake(self.page * WIDTH, HIGHT);
    self.bigSV.bounces = NO;
    self.bigSV.pagingEnabled = YES;
    self.bigSV.contentOffset = CGPointMake(WIDTH * self.myrow, 0);
    [self.view addSubview:self.bigSV];
}


//左侧按钮触发事件
-(void)leftButtonAction
{
    [self.navigationController popViewControllerAnimatedWithTransition:    UIViewAnimationTransitionFlipFromRight];
}

-(void)setVideoArray:(NSMutableArray *)videoArray
{
    _videoArray = videoArray;
    
    [self createBigSV];
    
    for (int i = 0; i < self.page; i++) {
        self.videoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 280 * XHIGHT)];
        [self.bigSV addSubview:self.videoImageView];
  //  XLImagePageView *xlImagePageView = [[XLImagePageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 280)];
//    xlImagePageView.delegate = self;

  //  xlImagePageView.imageArray = videoArray;
    self.testArray = [NSMutableArray array];
    for (int i = 0; i < self.page; i++) {
        self.videoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, 280 * XHIGHT)];
        
        
        [self.bigSV addSubview:self.videoImageView];
        [self.testArray addObject:self.videoImageView];
 
    
     
        
   //     [self.bigSV addSubview:xlImagePageView];
        self.videoImageView.userInteractionEnabled = YES;
        self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.startButton.frame = CGRectMake(0, 0, 80 * XWIDTH, 80 * XWIDTH);
        self.startButton.center = CGPointMake(WIDTH / 2, 140 * XHIGHT);
        [self.startButton setImage:[UIImage imageNamed:@"iconfont-bofang.png"] forState:0];
        self.titleLabel.textColor = [UIColor whiteColor];
        [self.startButton addTarget:self action:@selector(startButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.startButton.tag = 3000 + i;
        [self.videoImageView addSubview:self.startButton];
        
        
        
        self.bigImageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 280 * XHIGHT, WIDTH, HIGHT - 280 * XHIGHT)];
        [self.bigSV addSubview:self.bigImageView];
        
        self.tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bigImageView.frame.size.width, self.bigImageView.frame.size.height)];
        self.tempView.backgroundColor = [UIColor blackColor];
        self.tempView.alpha = 0.4;
        [self.bigImageView addSubview:self.tempView];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * XWIDTH, 20 * XWIDTH, WIDTH - 20 * XWIDTH, 30 * XHIGHT)];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:19 * XWIDTH];
        [self.bigImageView addSubview:self.titleLabel];
        
        self.tiaoView = [[UIView alloc] initWithFrame:CGRectMake(20 * XWIDTH, 50 * XHIGHT, 100 * XWIDTH, 1)];
        self.tiaoView.backgroundColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
        [self.bigImageView addSubview:self.tiaoView];
        
        self.categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * XWIDTH, 80 * XHIGHT, WIDTH, 20 * XHIGHT)];
        self.categoryLabel.textColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
        self.categoryLabel.font = [UIFont systemFontOfSize:15 * XWIDTH];
        [self.bigImageView addSubview:self.categoryLabel];
        
        self.detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(10 * XWIDTH, 120 * XHIGHT, WIDTH - 20 * XWIDTH, 0)];
        self.detailLabel.font = [UIFont systemFontOfSize:15 * XWIDTH];
        self.detailLabel.textColor = [UIColor colorWithRed:231 / 255.0 green:231 / 255.0 blue:231 / 255.0 alpha:1];
        self.detailLabel.numberOfLines = 0;
        [self.bigImageView addSubview:self.detailLabel];
        
        self.aVideo = [self.videoArray objectAtIndex:i];
        [self.videoImageView sd_setImageWithURL:[NSURL URLWithString:self.aVideo.coverForFeed]];
        [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:self.aVideo.coverBlurred]];
        self.titleLabel.text = self.aVideo.title;
        [self.titleLabel sizeToFit];
        self.tiaoView.frame = CGRectMake(15 * XWIDTH, 60 * XHIGHT, self.titleLabel.frame.size.width - 40 * XWIDTH, 1);
        self.categoryLabel.text = [NSString stringWithFormat:@"#%@",self.aVideo.category];
        self.detailLabel.text = self.aVideo.detail;
        [self.detailLabel sizeToFit];
        
        self.bigImageView.userInteractionEnabled = YES;
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(10 * XWIDTH, 240 * XHIGHT, 50 * XWIDTH, 50 * XWIDTH);
//        button.tag = 5000 + i;
//        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.bigImageView addSubview:button];
//        [button setShowsTouchWhenHighlighted:YES];
        
//        UILabel *shoucangLabel = [[UILabel alloc] initWithFrame:CGRectMake(23 * XWIDTH, 275 * XHIGHT, 50 * XWIDTH, 25 * XHIGHT)];
//        shoucangLabel.text = @"收藏";
//        shoucangLabel.textColor = [UIColor whiteColor];
//        shoucangLabel.font = [UIFont systemFontOfSize:12 * XWIDTH];
//        [self.bigImageView addSubview:shoucangLabel];
//        
        
//        UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
//        shareButton.frame = CGRectMake(75 * XWIDTH, 240 * XHIGHT, 50 * XWIDTH, 50 * XWIDTH);
//        [shareButton setImage:[UIImage imageNamed:@"app_zj_fx@2x.png"] forState:0];
//        [self.bigImageView addSubview:shareButton];
//        [shareButton setShowsTouchWhenHighlighted:YES];
//        [shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//        shareButton.tag = 6000 + i;
//        
//        UILabel *fenxiangLabel = [[UILabel alloc] initWithFrame:CGRectMake(88 * XWIDTH, 275 * XHIGHT, 50 * XWIDTH, 25 * XHIGHT)];
//        fenxiangLabel.text = @"分享";
//        fenxiangLabel.textColor = [UIColor whiteColor];
//        fenxiangLabel.font = [UIFont systemFontOfSize:12 * XWIDTH];
//        [self.bigImageView addSubview:fenxiangLabel];
        
        
      //  NSMutableDictionary *myDic = [[DataHandle shareDataHandle] selectVieo];
    //    self.video = [myDic objectForKey:self.aVideo.title];
#warning 改button样式
//        if (self.video) {
//            [button setImage:self.shoucangImage forState:0];
//    
//            self.shoucang = YES;
//        }
//        else
//        {
//            [button setImage:self.quxiaoshoucangImage forState:0];
//            self.shoucang = NO;
//        }
    }
}
    

   

}







-(void)startButtonAction:(UIButton *)button
{

    //音乐暂停通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"shipin" object:nil];
    
//    NSLog(@"button = %@",button);
    
    Video *avideo = [self.videoArray objectAtIndex:button.tag - 3000];
    
//    NSLog(@"URL = %@",avideo.playUrl);
  //  self.moviePlayer = [[BaseMVViewController alloc] initWithContentURL:[NSURL URLWithString:avideo.playUrl]];
    MoviePlayerViewController *av = [[MoviePlayerViewController alloc] initNetMovieWithURL:[NSURL URLWithString:avideo.playUrl]];
    
    
    
   // [self.moviePlayer.moviePlayer prepareToPlay];
    //模态
       [self presentViewController:av animated:YES completion:nil];
//    [self.moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
//    [self.moviePlayer.view setBackgroundColor:[UIColor clearColor]];
//    [self.moviePlayer.view setFrame:self.view.bounds];
}
-(void)movieStateChangeCallback:(NSNotification*)notify  {
    //点击播放器中的播放/ 暂停按钮响应的通知
    
    NSLog(@"播放or暂停");
}
-(void)movieFinishedCallback:(NSNotification*)notify{
    // 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Done按钮被点击响应的通知。
    NSLog(@"返回");
    
}

#warning 点击收藏按钮触发事件
//点击收藏按钮触发事件
-(void)buttonAction:(UIButton *)button
{
    NSInteger num = (NSInteger)self.bigSV.contentOffset.x / WIDTH;
    self.myvideo = [self.videoArray objectAtIndex:num];
    
    if (self.shoucang == NO) {

        [button setImage:self.shoucangImage forState:0];
        [[DataHandle shareDataHandle] insertVideoTable:self.myvideo];

        self.shoucang = YES;
    }
    else if (self.shoucang == YES)
    {
        NSLog(@"%@",self.myvideo.title);

        [button setImage:self.quxiaoshoucangImage forState:0];
        [[DataHandle shareDataHandle] deleteVideo:self.myvideo.title];
        self.shoucang = NO;
    }

}

-(void)changeColoring:(NSNotification *)notification
{
#warning 改的颜色
    self.atitleLabel.textColor = [UIColor blackColor];
    
    NSString *string = [notification object];
    if ([string isEqualToString:@"no"]) {
        
#warning 原有颜色
        self.atitleLabel.textColor = [UIColor whiteColor];
    }
    //保存当前状态值
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"nighting"];
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
